package com.heima.article.listener;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.pojos.WmNews;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class ArticleIsDownListener {

    @Autowired
    ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void articleIsDownListener(ConsumerRecord<?,?> record){
        Object value = record.value();
        String valueStr = (String) value;
        WmNews wmNews = JSON.parseObject(valueStr, WmNews.class);

        Long articleId = wmNews.getArticleId();
        Short enable = wmNews.getEnable();
        if (enable.equals((short)1)){
            enable = 0;
        }else {
            enable = 1;
        }
        apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
        .eq(ApArticleConfig::getArticleId,articleId)
                .set(ApArticleConfig::getIsDown,enable));
    }

}
